<?php
 /**
 +------------------------------------------------------------------------------
 * FramkPHP 快速易用PHP框架
 +------------------------------------------------------------------------------
 * @package  Framk
 * @author   shawn fon <ucic5@163.com>
 +------------------------------------------------------------------------------
 */
 
//定义框架目录常量
	define ( 'FRAMK_VERSION', '1.8.131201' );
	define ( 'FRAMK', dirname(__FILE__).S);
	$appFolderName = array ('Action','Cache', 'Extend','View');
	foreach ( $appFolderName as $key=>$value ) {define ( strtoupper ( $value ), APP_ROOT.$value.S );}
	require_once (FRAMK . '_Function.php');//引入框架内置函数
	if (PHP_VERSION <5) _error('PHP_Version',PHP_VERSION);
		
//引入配置文件	
	$GLOBALS['C']= require_once(EXTEND .'Config.php');
	$GLOBALS['C']['Debug'] ? error_reporting ( E_ALL ) : error_reporting ( 0 );
	date_default_timezone_set ($GLOBALS['C']['TimeZone']);
	$GLOBALS['FramkClass']=array('Action','Cache','View' ,'DB','Mysql','Page');//已有的框架类	
	
//引入框架核心文件
	_import(FRAMK.'Action.class.php');	
	_import(FRAMK.'Cache.class.php');
	_import(FRAMK.'View.class.php');

	function RunFramk() {
	
//定义与项目名称相对路径APP、URL访问前缀ACT
		$scriptName=SCRIPT_NAME==='' ? $_SERVER ['SCRIPT_NAME'] : SCRIPT_NAME ;
		$app=str_replace ( '/'.basename($scriptName), '', $scriptName );//项目相对路径，如/demo 	
		$act=$GLOBALS['C']['ReWrite'] ? $app : $scriptName;	
		define ( 'APP', 'http://'.$_SERVER["HTTP_HOST"].$app );
		define ( 'ACT', 'http://'.$_SERVER["HTTP_HOST"].$act);

//访问地址解析
		$urlStr=( $_SERVER ['REQUEST_URI'] == $app.'/')? '': str_replace ($act, '',  $_SERVER ['REQUEST_URI'] );
		if(!empty($_SERVER['QUERY_STRING'])){//将QUERY_STRING模式重组为PATH_INFO模式
			$queryArr=explode('&',$_SERVER['QUERY_STRING']);$queryStr='';
			foreach($queryArr as $v){$queryStr .=preg_replace('/(.*)\=/','/',$v);}
			$urlStr=str_replace('?'.$_SERVER['QUERY_STRING'],'',$urlStr).$queryStr;
		}
		$urlStr=ltrim(urldecode($urlStr),'/');//此时解编码，防止在上一步解析时将“网址”作为参数的访问地址也作解析
		$urlArr=explode('/',$urlStr);
		//若设置了伪静态则将伪静态后缀删除
		$ext=end($urlArr);
		if(strpos($ext,$GLOBALS['C']['HtmlExt']))array_splice($urlArr, -1, 1, str_replace($GLOBALS['C']['HtmlExt'], '', $ext));

//获取Action目录,第一个字母小写被认为是目录名		
		$actionDir='';	
		foreach($urlArr as $key=>$value){
			if(strcmp($value ,ucfirst($value))>0){
				$actionDir .=$value.'/' ;	
				unset($urlArr[$key]);
			}else{
				break;
			}			
		}	
		$urlArr=array_merge($urlArr,array());	//重新排序
		
//取得动作方法
		$action=$GLOBALS['C']['Action'];//默认Action
		$method = $GLOBALS['C']['Method'];
		if(!empty( $urlArr[0] ))$action=$urlArr[0];
		if(!empty( $urlArr[1] ))$method = $urlArr[1];
		
		$len=count($urlArr);$paramArr=array();
		if($len>2)$paramArr=array_splice($urlArr,2);//取得参数
		$params=array_merge(array(''),$paramArr);//使参数取值时以键为1开始便于理解，如$p[1],取第一个参数的值
		$_GET['P']=$params;  $_GET['URL_']= implode('/',$params);//参数数组与参数字符串	

		$dir_action_method = empty( $actionDir) ? array($action,$method) :array($actionDir,$action,$method);
		if(!empty($paramArr)){
			$_GET['URL_ARR'] = array_merge($dir_action_method,$paramArr);//包括访问动作与方法的路径数组
		}else{
			$_GET['URL_ARR'] = $dir_action_method;
		}
		$_GET['URL']=ACT.'/'.implode('/',$_GET['URL_ARR']); //完整的访问路径字符串
		$_GET['ACTION'] = $action ; $_GET['METHOD'] = $method ;

//实例化Action类并访问其方法
		$ActObj = _class(ACTION.$actionDir.$action);
		if (method_exists ($ActObj, $method )) {	
			$ActObj->$method();
		} else {
			_error('methodNotExist', $action.'—>'.$method);
		}	
	}			
	
 /*  +------------------------------------------------------------------------------ */
?>